import { getHomeGoodPriceData } from "@/services";
import { createSlice, createAsyncThunk } from "@reduxjs/toolkit";

export const fetchHomeDataAction = createAsyncThunk("fetchdata", async (payload) => {
  const res = await getHomeGoodPriceData()
  return res
})

const homeSlice = createSlice({
  name: "home",
  initialState: {
    productList: [],
    goodPriceInfo: {}
  },
  reducers: {
    changeGoodPriceInfoAction(state, { payload }) {
      state.goodPriceInfo = payload;
    }
  },
  extraReducers: (builder) => {
    builder
      .addCase(fetchHomeDataAction.fulfilled, (state, action) => {
        console.log(action.payload);
        state.goodPriceInfo = action.payload;
      })
      // 如果你需要处理其他状态（如 pending, rejected），也可以添加它们
      .addCase(fetchHomeDataAction.pending, (state) => {
        // 可以设置一些加载状态
      })
      .addCase(fetchHomeDataAction.rejected, (state, action) => {
        // 处理错误
        console.error(action.error);
      });
  }
  // extraReducers: {
  //   [fetchHomeDataAction.fulfilled](state, { payload }) {
  //     console.log(payload);
  //     state.goodPriceInfo = payload;
  //   }
  // }
});

export const { changeGoodPriceInfoAction } = homeSlice.actions;
export default homeSlice.reducer;